package seed.web.servlet;


import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import seed.domain.Comment;
import seed.domain.Message;
import seed.domain.PageBean;
import seed.domain.Seed;
import seed.service.SeedService;
import user.domain.User;
import cn.itcast.commons.CommonUtils;

import cn.itcast.servlet.BaseServlet;


public class SeedServlet extends BaseServlet {
	 SeedService seedService = new SeedService ();
	 
	
	 
	  /**
	   * 鍒楀嚭鎵�鏈夌瀛�
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	    public String findAll(HttpServletRequest request, HttpServletResponse response)
	    		throws ServletException, IOException {
	    	/*
	    	 * 1.鑾峰彇椤甸潰浼犻�掔殑pc
	    	 * 2.缁欏畾pc鐨勫��
	    	 * 3.浣跨敤pc鍜宲s璋冪敤service鏂规硶锛屽緱鍒癙ageBean锛屼繚瀛樺埌request鍩�
	    	 * 4.杞彂鍒發ist.jsp
	    	 */
	    	int pc = getPc(request);//寰楀埌PC
	    	int ps = 4;//姣忛〉4琛岃褰�
	    	PageBean<Seed> pb = seedService.findAll(pc,ps);//浼犻�抪c,ps缁檚ervice
	    	request.setAttribute("pb", pb);//淇濆瓨鍒皉equest鍩熶腑
	    	//request.setAttribute("seedList", seedService.findAll());
	    	return "f:/list.jsp";
	    }
	    /**
	     * 鑾峰彇PC
	     * @param request
	     * @return
	     */
	    public int getPc(HttpServletRequest request){
	    	String value = request.getParameter("pc");
	    	if(value == null || value.trim().isEmpty()){
	    		return 1;
	    	}
	    	return Integer.parseInt(value);
	    }
	    /**
	     * 鍔犺浇璇︽儏椤电瀛�
	     * @param request
	     * @param response
	     * @return
	     * @throws ServletException
	     * @throws IOException
	     * @throws SQLException 
	     */
	    public String load(HttpServletRequest request, HttpServletResponse response)
	    		throws ServletException, IOException, SQLException {
	    	/*
	    	 * 1.寰楀埌鍙傛暟sid
	    	 * 2.鏌ヨ寰楀埌seed
	    	 * 3.淇濆瓨鍒癲etail.jsp
	    	 */
	    	HttpSession session = request.getSession();
	    	User  curUser = (User)session.getAttribute("session_user");
	    	String sid = request.getParameter("sid");
	    	
	    	if(curUser != null)
	    		seedService.addPersonalRecommend(curUser.getUid(), sid);
	    	
	    	seedService.updateOverNumber(sid);
	    	List<Comment> commentMessage = seedService.commentMessage(sid);
			    	
			for(Comment comment: commentMessage)
			{
				User user = seedService.getUserByUid(comment.getUid());
				comment.setUser(user);
			}
			
			request.setAttribute("commentmMessage", commentMessage);
	    	request.setAttribute("seed", seedService.load(request.getParameter("sid")));
	    	
			return "f:/detail.jsp";
	    }
	    /**
	     * 搜索方法
	     */
	    public String query(HttpServletRequest request, HttpServletResponse response)
	    		throws ServletException, IOException {
	    	
	    	String sname = new String(request.getParameter("sname").getBytes("iso8859-1"),"UTF-8");
	    	
	    	Seed creatia = CommonUtils.toBean(request.getParameterMap(), Seed.class);
	    	//String sname = request.getParameter("sname");
	    	
	    	List<Seed> listSeed = (List<Seed>) seedService.query(creatia);
	    	for(Seed s: listSeed)
	    	{
	    		//System.out.println(s.getSname());
	    	}
	    	PageBean<Seed> pb = new PageBean<Seed>();
	    	pb.setBeanList(listSeed);
	    	pb.setPc(1);
	    	pb.setPs(6);
	    	pb.setTr(12);
	    	request.setAttribute("pb", pb);
	    
	    	return "f:/list.jsp";
	    	
	    }
	 /*   public String findBysname(HttpServletRequest request, HttpServletResponse response)
	    		throws ServletException, IOException {
	    	String sname = request.getParameter("sname");
	    	Seed seed = seedService.query(sname);
	    	request.setAttribute("seed", seed);
			return "f:/list.jsp";
	    }*/
	    /**
		 * 鎸夊垎绫绘煡璇㈢瀛�
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws IOException
		 */
		public String findByCategory(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String cid = request.getParameter("cid");
			request.setAttribute("seedList", seedService.findByCategory(cid,1,5));
			return "f:/c_list.jsp";
		}
		/**
		 * 瀵规瘮锛堥噸鐐癸級
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws IOException
		 */
		public void compare(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			
			String sid = request.getParameter("sid");
			HttpSession session = request.getSession(true);
			response.setCharacterEncoding("utf-8");
			
			List<String> compare = (List<String>) (session.getAttribute("compare") == null?new ArrayList<String>():session.getAttribute("compare"))  ;
			int size = compare.size();
			if( size == 4)
			{
				response.getWriter().print("<p>(4/4)加入对比已满,可以<a href='SeedServlet?method=compareShow'>点击这里</a>查看</p>");
			}
			else if(!compare.contains(sid))
			{
				compare.add(sid);
				session.setAttribute("compare", compare);
				response.getWriter().print("<p>("+compare.size()+"/4)已把该商品加入对比,可以<a href='SeedServlet?method=compareShow'>点击这里</a>查看</p>");
			}
				
			else 
			{
				response.getWriter().print("<p>您已加入该商品("+compare.size()+"/4)可以<a href='SeedServlet?method=compareShow'>点击这里</a>查看</p>");
			
			}
			
			
		}
		public String deleteCompare(HttpServletRequest request, HttpServletResponse response)
		{
			HttpSession session = request.getSession();
			List<String> compare = (List<String>) session.getAttribute("compare");
			if(compare == null)
				return "f:/SeedServlet?method=compareShow";
			String sid = request.getParameter("sid");
			if(sid == null) 
				return "f:/SeedServlet?method=compareShow";
			
			boolean f = false;
			for(String c: compare)
			{
				if(c.equals(sid))
					f = true;
			}
			if(f)
			{
				compare.remove(sid);
			}
			for(String c:compare)
			{
				System.out.println(c);
			}
			return"f:/SeedServlet?method=compareShow";
		}
		
		public String compareShow(HttpServletRequest request, HttpServletResponse response)
		{
			HttpSession session = request.getSession(true); 
			List<String> compare = (List<String>) session.getAttribute("compare");
			List<Seed> seedlist = new ArrayList<Seed>();
			
			for(String sid:compare)
			{
				Seed seed= seedService.getSeedBySid(sid);
				seedlist.add(seed);
			}
			
			request.setAttribute("seedlist", seedlist);
			return "f:/compare.jsp";
		}
		/**
		 * message页面
		 * 
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws SQLException 
		 */
		public String messageGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, SQLException
		{
			
			List<Message> listmessage = seedService.listMessage();
			
			for(Message message: listmessage)
			{
				User user = seedService.getUserByUid(message.getUid());
				message.setUser(user);
			}
			
			request.setAttribute("listmessage", listmessage);
			return "f:/message.jsp";
		}
		
		public String messageAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, SQLException
		{
			HttpSession session = request.getSession();
			if(session == null || session.getAttribute("session_user") == null)
				return "f:/login.jsp";
			else
			{
				Message message = CommonUtils.toBean(request.getParameterMap(), Message.class);
				message.setMessageId(CommonUtils.uuid());
				Date now = new Date();
				message.setTime(now);
				User user = (User) session.getAttribute("session_user");
				String uid = user.getUid();
				System.out.println(uid);
				message.setUid(uid);
				request.setAttribute("listMessage", seedService.addMessage(message));
				
				
			}
						
			return messageGet(request, response);
			
		}
		
			
		public String commentAdd(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException
			{
				HttpSession session = request.getSession();
				if(session == null || session.getAttribute("session_user") == null)
					return "f:/login.jsp";
				else
				{
					Comment comment = CommonUtils.toBean(request.getParameterMap(), Comment.class);
					comment.setCommentId(CommonUtils.uuid());
					Date now = new Date();
					comment.setTime(now);
					User user = (User) session.getAttribute("session_user");
					String uid = user.getUid();
					comment.setUid(uid);			
					seedService.addComment(comment);
				}
				
				return "r:/SeedServlet?method=load&sid="+request.getParameter("sid");
				
				
			}
		//////
		//////
		//////
		/*
		 * 后台方法
		 */
		
	    
}
